#ifdef RT_USE_OLD_CONN_WRAP

#if !defined RLB_TCP_H  && !defined (_NEW_PROTO_TP)
#define RLB_TCP_H

#include "RtCnBase.h"

#define SEND_BUF_LEN_MAX4RLB		8192 		// 8K bytes 

#define MAX_TIMES_NO_RECV			6			//For Keep Alive or Data recv Checking(MAX_TIMES_NO_RECV*INTERVAL2 = 1 Minute)
#define MAX_TRY_CONNECT				6			//For Conn-Resp Checking
#define TIMEOUT_FOR_CONNECTION		(double)10

class CRlbConnTCPClient;
class CRlbConnTCPServer;

typedef ServerListT<CRlbConnTCPServer>			CRlbConnTCPServerList;

typedef CConnAcceptorSinkT<CRlbConnTCPServer>	CRlbConnTCPAcceptorSink;
typedef CConnAcceptorT<CRlbConnTCPServer>		CRlbConnTCPAcceptor;

typedef CConnConnectorT<CRlbConnTCPClient>		CRlbConnTCPConnector;

//For Upper layer
//Act as a Client IRtTransport obj to Upper layer, 
//and also as a IRtTransportSink sink into TP layer.
class /*RT_API_EXPORT*/ CRlbConnTCPClient : public CRtCnConn
{
public:
	// interface IRtAcceptorConnectorSink
	virtual void OnConnectIndication(
		RtResult aReason,
		IRtTransport *aTrpt,
		IRtAcceptorConnectorId *aRequestId);

	virtual void OnDisconnect(
		RtResult aReason,
		IRtTransport *aTrptId);
	
	virtual void OnTimer(CRtTimerWrapper* aId);

	virtual RtResult Disconnect(RtResult aReason);

	virtual RtResult GetOption(
		DWORD aCommand, 
		LPVOID aArg);
public:
	//Client will do Reconnection  
	void SetConnConnector(CRlbConnTCPConnector* pConnConnector);

	//Cancel connect call, stop the handshake if connection hasn't made
	void CancelHandShake();
	void ScheduleConnectTimer(const CRtTimeValue& aTimeout) {}

protected:
	//Internal functions	
	virtual void Reconnect();
	virtual void OnRecvConnResp();
	virtual void OnRecvDisconn();
	void Reset();
	void Reset4Reconnect();

public:
	CRlbConnTCPClient(DWORD dwSendBuffSize = SEND_BUF_LEN_MAX4RLB);
	virtual ~CRlbConnTCPClient();
protected:
	int m_nUngetDataCnt;//Counter for times haven't received data
//	int m_nReconnect;	//Max times for reconnection

	CRtAutoPtr<CRlbConnTCPConnector> m_pConnConnector;			//Only Client use
	BOOL m_bConnectOnceMade;	//Indicate if Connection once made
//	WORD m_wMaxReconn;			//Max times to reconnect

	BOOL m_bHandShakeCancelled;
	LONGLONG	m_dwDisconnTick;
	CRtTimerWrapper	m_ReconnectTimer;
	CRtTimerWrapper m_bwctlTimer;
};

//For Upper layer
//Act as a Server IRtTransport obj to Upper layer, 
//and also as a IRtTransportSink sink into TP layer.
class /*RT_API_EXPORT*/ CRlbConnTCPServer : public CRtCnConn
{
public:
	virtual void OnDisconnect(
		RtResult aReason,
		IRtTransport *aTrptId);
	
	virtual void OnTimer(CRtTimerWrapper* aId);

	// interface IRtAcceptorConnectorSink
	virtual void OnConnectIndication(
		RtResult aReason,
		IRtTransport *aTrpt,
		IRtAcceptorConnectorId *aRequestId);

	virtual RtResult Disconnect(RtResult aReason);
	
	virtual RtResult GetOption(
		DWORD aCommand, 
		LPVOID aArg);

	virtual RtResult SetOption(
		DWORD aCommand, 
		LPVOID aArg);

public:
	//Unconnected time stamp
	CRtTimeValue GetDisconnTimestamp();
	void TryOnDisconnIndicate();
	void OnRecvConnReq(CRtCnPduConnReqResp& preq);
protected:
	virtual void OnRecvConnReq();
	void Reset();
	void Reset4ReconnComing();
public:
	CRlbConnTCPServer(DWORD dwSendBuffSize = SEND_BUF_LEN_MAX4RLB);
	virtual ~CRlbConnTCPServer();

	//In order to keep one instance of Server List.
	void SetServerList(CRlbConnTCPServerList *pSvrList);

	void SetConnAcceptor(CRlbConnTCPAcceptor* pConnAcceptor);
protected:
	//When Reconnecting, new coming connection may 
	////Attach some handles & states to the old one
	void Attach(CRlbConnTCPServer* pSvr);
protected:
	CRlbConnTCPServerList* m_pSvrList;
	CRtAutoPtr<CRlbConnTCPAcceptor> m_pConnAcceptor;
	CRtTimerWrapper m_ReleaseTimer;
	CRtTimerWrapper m_bwctlTimer;
};

#endif // RLB_TCP_H

#endif // RT_USE_OLD_CONN_WRAP
